import { Injectable, LoggerService as NestLoggerService } from '@nestjs/common'
import { ConfigService } from '@nestjs/config'
import * as winston from 'winston'
import 'winston-daily-rotate-file'

@Injectable()
export class LoggerService implements NestLoggerService {
    private logger: winston.Logger

    constructor(private configService: ConfigService) {
        const env = this.configService.get('NODE_ENV')
        const logDir = this.configService.get('LOG_DIR', 'logs')

        this.logger = winston.createLogger({
            level: env === 'production' ? 'info' : 'debug',
            format: winston.format.combine(
                winston.format.timestamp(),
                winston.format.json()
            ),
            defaultMeta: { service: 'comfy-api' },
            transports: [
                // 控制台输出
                new winston.transports.Console({
                    format: winston.format.combine(
                        winston.format.colorize(),
                        winston.format.simple()
                    )
                }),
                // 按日期轮转的文件日志
                new winston.transports.DailyRotateFile({
                    filename: `${logDir}/error-%DATE%.log`,
                    datePattern: 'YYYY-MM-DD',
                    level: 'error',
                    maxFiles: '30d'
                }),
                new winston.transports.DailyRotateFile({
                    filename: `${logDir}/combined-%DATE%.log`,
                    datePattern: 'YYYY-MM-DD',
                    maxFiles: '30d'
                })
            ]
        })
    }

    log(message: string, context?: string) {
        this.logger.info(message, { context })
    }

    error(message: string, trace?: string, context?: string) {
        this.logger.error(message, { trace, context })
    }

    warn(message: string, context?: string) {
        this.logger.warn(message, { context })
    }

    debug(message: string, context?: string) {
        this.logger.debug(message, { context })
    }

    verbose(message: string, context?: string) {
        this.logger.verbose(message, { context })
    }
} 